//
//  JscjV.swift
//  Auscultation
//
//  Created by 来 on 2020/12/10.
//

import UIKit

class JscjV: UIView {
    
    var title: String = "请完善信息以保存"
    
    var back_view: UIView!
    var collectionView: UICollectionView!
    
    var xb_i1: UIImageView!
    var xb_i2: UIImageView!

    var sg: LGFLoginImageTitleView!
    var tz: LGFLoginImageTitleView!
    var tw: LGFSegmentView!
    var phcd: LGFSegmentView!
    var sfzj: LGFSegmentView!
    var sfss: LGFSegmentView!
    var cjcs: LGFLoginImageTitleView!
    
    var xb: String = "男"
    var sbly: LGFLoginImageTitleView!
    var cjdz: LGFLoginImageTitleView!
    
    var twIdArr: [String]!
    var twArr: [String]!
    var phcdIdArr: [String]!
    var phcdArr: [String]!
    var cjcsIdArr: [String]!
    var cjcsArr: [String]!
    
    var save: (([String: String]) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    func initUI() {
        
        backgroundColor = UIColor.black.withAlphaComponent(0.2)
        
        back_view = UIView.init()
        back_view.layer.cornerRadius = 8
        back_view.backgroundColor = UIColor.white
        addSubview(back_view)
        back_view.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.8)
            make.height.equalTo(410)
        }
        
        back_view.transform = CGAffineTransform.init(scaleX: 0.8, y: 0.8)
        self.backgroundColor = UIColor.black.withAlphaComponent(0)
        UIView.animate(withDuration: 0.2) {
            self.back_view.transform = CGAffineTransform.identity
            self.backgroundColor = UIColor.black.withAlphaComponent(0.2)
        } completion: { (finish) in
        }
        
        let label1 = UILabel.init()
        label1.text = title
        label1.textAlignment = .center
        label1.font = UIFont.boldSystemFont(ofSize: 16)
        label1.textColor = UIColor.init(lgf_HexString: "222222")
        back_view.addSubview(label1)
        label1.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(40)
        }
        
        let line1 = UIView.init()
        line1.backgroundColor = UIColor.init(lgf_HexString: "F1F2F4")
        back_view.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.top.equalTo(label1.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        // 身高
        sg = LGFLoginImageTitleView.init(nil, nil, left: 70, placeholder: "身高(cm)", text: "", false)
        sg.textField.keyboardType = .numberPad
        back_view.addSubview(sg)
        sg.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(line1.snp.bottom)
            make.height.equalTo(40)
        }
        let sg_l = UILabel.init()
        sg_l.text = "身高(cm)"
        sg_l.textColor = UIColor.init(lgf_HexString: "222222")
        sg_l.font = UIFont.boldSystemFont(ofSize: 16)
        sg.addSubview(sg_l)
        sg_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        let sg_x = UILabel.init()
        sg_x.text = "*"
        sg_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
        sg_x.font = UIFont.boldSystemFont(ofSize: 16)
        sg.addSubview(sg_x)
        sg_x.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview().offset(2)
        }
        
        tz = LGFLoginImageTitleView.init(nil, nil, left: 70, placeholder: "体重(kg)", text: "", false)
        tz.textField.keyboardType = .numberPad
        back_view.addSubview(tz)
        tz.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(sg.snp.bottom)
            make.height.equalTo(40)
        }
        let tz_l = UILabel.init()
        tz_l.text = "体重(kg)"
        tz_l.textColor = UIColor.init(lgf_HexString: "222222")
        tz_l.font = UIFont.boldSystemFont(ofSize: 16)
        tz.addSubview(tz_l)
        tz_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        let tz_x = UILabel.init()
        tz_x.text = "*"
        tz_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
        tz_x.font = UIFont.boldSystemFont(ofSize: 16)
        tz.addSubview(tz_x)
        tz_x.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview().offset(2)
        }
        
        tw = segmentView(tz, "体位", self.twArr)
        
        phcd = segmentView(tw.superview!, "配合程度", self.phcdArr)
        
        sfzj = segmentView(phcd.superview!, "是否镇静", ["否", "是"])
        
        sfss = segmentView(sfzj.superview!, "是否手术", ["否", "是"])
        
        cjcs = LGFLoginImageTitleView.init(nil, #imageLiteral(resourceName: "icon_arrow"), left: 70, placeholder: "采集场所", text: "", true)
        cjcs.lgf_ViewName = "采集场所"
        cjcs.delegate = self
        back_view.addSubview(cjcs)
        cjcs.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(sfss.superview!.snp.bottom)
            make.height.equalTo(40)
        }
        let cjcs_l = UILabel.init()
        cjcs_l.text = "采集场所"
        cjcs_l.textColor = UIColor.init(lgf_HexString: "222222")
        cjcs_l.font = UIFont.boldSystemFont(ofSize: 16)
        cjcs.addSubview(cjcs_l)
        cjcs_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        let cjcs_x = UILabel.init()
        cjcs_x.text = "*"
        cjcs_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
        cjcs_x.font = UIFont.boldSystemFont(ofSize: 16)
        cjcs.addSubview(cjcs_x)
        cjcs_x.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview().offset(2)
        }
        
        cjdz = LGFLoginImageTitleView.init(nil, nil, left: 70, placeholder: "采集地址", text: "", false)
        back_view.addSubview(cjdz)
        cjdz.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(cjcs.snp.bottom)
            make.height.equalTo(40)
        }
        let cjdz_l = UILabel.init()
        cjdz_l.text = "采集地址"
        cjdz_l.textColor = UIColor.init(lgf_HexString: "222222")
        cjdz_l.font = UIFont.boldSystemFont(ofSize: 16)
        cjdz.addSubview(cjdz_l)
        cjdz_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        let line2 = UIView.init()
        line2.backgroundColor = UIColor.init(lgf_HexString: "F1F2F4")
        back_view.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.top.equalTo(cjdz.snp.bottom).offset(-1)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        let label2 = UILabel.init()
        label2.text = "取消"
        label2.textAlignment = .center
        label2.font = UIFont.boldSystemFont(ofSize: 18)
        label2.textColor = UIColor.red
        label2.lgf_AddTap(target: self, action: #selector(JscjV.quxiao(_:)))
        back_view.addSubview(label2)
        label2.snp.makeConstraints { (make) in
            make.top.equalTo(line2.snp.bottom)
            make.left.equalToSuperview()
            make.height.equalTo(50)
            make.width.equalToSuperview().multipliedBy(0.5)
        }
        
        let label7 = UILabel.init()
        label7.text = "保存"
        label7.textAlignment = .center
        label7.font = UIFont.boldSystemFont(ofSize: 18)
        label7.textColor = UIColor.init(lgf_HexString: "0F88F2")
        label7.lgf_AddTap(target: self, action: #selector(JscjV.baocun(_:)))
        back_view.addSubview(label7)
        label7.snp.makeConstraints { (make) in
            make.top.equalTo(line2.snp.bottom)
            make.right.equalToSuperview()
            make.height.equalTo(50)
            make.width.equalToSuperview().multipliedBy(0.5)
        }
    }
    
    func show(_ sv: UIView,_ title: String = "请完善信息以保存", _ call: @escaping (([String: String]) -> Void)) {
        self.frame = sv.bounds
        self.save = call
        self.title = title
        sv.addSubview(self)
        getData()
    }
    
    // 取消
    @objc func quxiao(_ sender: UITapGestureRecognizer) {
        save?([:])
        UIView.animate(withDuration: 0.2) {
            self.back_view.transform = CGAffineTransform.init(scaleX: 0.8, y: 0.8)
            self.back_view.alpha = 0.0
            self.backgroundColor = UIColor.black.withAlphaComponent(0)
        } completion: { (finish) in
            self.removeFromSuperview()
        }
    }
    
    @objc func baocun(_ sender: UITapGestureRecognizer) {
        if sg.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请输入身高")
            return
        }
        if sg.textField.text!.count > 3 {
            SVProgressHUD.showError("请输入正确身高")
            return
        }
        if tz.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请输入体重")
            return
        }
        if tz.textField.text!.count > 3 {
            SVProgressHUD.showError("请输入正确体重")
            return
        }
        if cjcs.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请输入采集场所")
            return
        }
        save?(["height": sg.textField.text ?? "",
               "weight": tz.textField.text ?? "",
               "position": self.twIdArr[tw.selectIndex],
               "position_s": self.twArr[tw.selectIndex],
               "coordinate": self.phcdIdArr[phcd.selectIndex],
               "coordinate_s": self.phcdArr[phcd.selectIndex],
               "calm": sfzj.selectIndex.description,
               "operation": sfss.selectIndex.description,
               "site": cjcs.lgf_Name ?? "",
               "site_s": cjcs.textField.text ?? "",
               "collectionAddress": cjdz.textField.text ?? ""])
        UIView.animate(withDuration: 0.2) {
            self.back_view.transform = CGAffineTransform.init(scaleX: 0.8, y: 0.8)
            self.back_view.alpha = 0.0
            self.backgroundColor = UIColor.black.withAlphaComponent(0)
        } completion: { (finish) in
            self.removeFromSuperview()
        }
    }
    
    func segmentView(_ view: UIView, _ title: String, _ data: [String]) -> LGFSegmentView {
        let tw = LGFLoginImageTitleView.init(nil, nil, left: 80, placeholder: "", text: "", true)
        back_view.addSubview(tw)
        tw.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(view.snp.bottom)
            make.height.equalTo(40)
        }
        let tw_l = UILabel.init()
        tw_l.text = title
        tw_l.textColor = UIColor.init(lgf_HexString: "222222")
        tw_l.font = UIFont.boldSystemFont(ofSize: 16)
        tw.addSubview(tw_l)
        tw_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        let tw_x = UILabel.init()
        tw_x.text = "*"
        tw_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
        tw_x.font = UIFont.boldSystemFont(ofSize: 16)
        tw.addSubview(tw_x)
        tw_x.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview().offset(2)
        }
        
        let segment = LGFSegmentView.init()
        segment.layer.cornerRadius = 13
        tw.addSubview(segment)
        segment.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.equalTo(26)
            make.width.equalTo(120)
        }
        
        DispatchQueue.main.async {
            segment.setData(data: data, font: UIFont.systemFont(ofSize: 14), selectColor: UIColor.init(lgf_HexString: "0F88F2"), unSelectColor: UIColor.init(lgf_HexString: "E0E6EB"), sTextColor: UIColor.white, unsTextColor: UIColor.white, dx: false)
        }
        
        return segment
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func getData() {
        let paramet: [String: Any] = [
            "groupCode": "SAMPLE_POSITION",
        ]
        EWERequest.lgf("dictionary", paramet, .get) { [weak self] (data) in
            self?.twIdArr = (data["result"] as? [[String: Any]] ?? []).map({ (dict) -> String in
                return (dict["id"] as? String) ?? ""
            })
            self?.twArr = (data["result"] as? [[String: Any]] ?? []).map({ (dict) -> String in
                return (dict["title"] as? String) ?? ""
            })
            let paramet2: [String: Any] = [
                "groupCode": "SAMPLE_COORDINATE ",
            ]
            EWERequest.lgf("dictionary", paramet2, .get) { [weak self] (data) in
                self?.phcdIdArr = (data["result"] as? [[String: Any]] ?? []).map({ (dict) -> String in
                    return (dict["id"] as? String) ?? ""
                })
                self?.phcdArr = (data["result"] as? [[String: Any]] ?? []).map({ (dict) -> String in
                    return (dict["title"] as? String) ?? ""
                })
                let paramet3: [String: Any] = [
                    "groupCode": "SAMPLE_SITE",
                ]
                EWERequest.lgf("dictionary", paramet3, .get) { [weak self] (data) in
                    self?.cjcsIdArr = (data["result"] as? [[String: Any]] ?? []).map({ (dict) -> String in
                        return (dict["id"] as? String) ?? ""
                    })
                    self?.cjcsArr = (data["result"] as? [[String: Any]] ?? []).map({ (dict) -> String in
                        return (dict["title"] as? String) ?? ""
                    })
                    self?.initUI()
                } _: { (error) in
                    SVProgressHUD.showError(error)
                }
            } _: { (error) in
                SVProgressHUD.showError(error)
            }
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
    }
    
}

extension JscjV: LGFLoginImageTitleViewDelegate {
    func selectLGFLoginImageTitleView(_ view: LGFLoginImageTitleView) {
        self.endEditing(true)
        if view.lgf_ViewName == "采集场所" {
            let picker = LGFBottomPickerView()
            let sindex = self.cjcsArr.lgf_IndexOfObject(self.cjcs.textField.text ?? "")
            picker.showIn(self.superview!, self.cjcsArr, sindex) { [weak self] (view, name, index) in
                self?.cjcs.textField.text = self?.cjcsArr[index]
                self?.cjcs.lgf_Name = self?.cjcsIdArr[index]
            }
        }
    }
}
